package controlleur.form;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import annuaire.beans.Promotion;
import annuaire.beans.Student;
import annuaire.rmi.remotedObject.IRemotedAnnuaire;
import annuaire.rmi.remotedObject.IRemotedUser;
import form.bean.StudentForm;

/**
 * Contrôleur responsable de l'ajout d'un etudiant par l'administrateur
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class AddStudentFormControlleur extends SimpleFormController {

	/** Logger pour cette classe ainsi que pour ses sous classes */
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * Reference vers l'objet distant Annuaire
	 */
	private IRemotedAnnuaire remotedAnnuaire;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object commandAjout,
			BindException errors) throws Exception {

		StudentForm s = (StudentForm) commandAjout;
		IRemotedUser user = (IRemotedUser) request.getSession().getAttribute(
				"user");
		logger.info("*******************************************************");
		logger.info(s.getIdPromo());
		logger.info("*******************************************************");

		Student newStudent = new Student();

		newStudent.setAddress(s.getAddress());
		newStudent.setComments(s.getComments());
		newStudent.setFirstName(s.getFirstName());
		newStudent.setName(s.getName());
		newStudent.setLogin(s.getLogin());
		newStudent.setMail(s.getMail());
		newStudent.setWeb(s.getWeb());
		newStudent.setSociety(s.getSociety());
		newStudent.setPwd(s.getPwd());

		

		if (!s.getIdPromo().equals("") && s.getIdPromo() != null) {
			int idp = Integer.parseInt(s.getIdPromo());
			Set<Student> set = new HashSet<Student>();
			set.add(newStudent);
			user.getAdminService().addStudentsToPromo(
					remotedAnnuaire.findPromoById(idp, false), set);
		}else{
			user.getAdminService().addStudent(newStudent);
		}

		ModelAndView mav = showNewForm(request, response);

		mav.addObject("succesStu", "Etudiant ajouté avec succes");
		return mav;
		// return new ModelAndView(getSuccessView(), "model", myModel);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws ServletException {

		return new StudentForm();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#referenceData
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Map<String, Object> referenceData(HttpServletRequest request)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();

		Collection<Integer> listIdPromo = remotedAnnuaire.findAllPromosId();
		List<Promotion> listPromo = new ArrayList<Promotion>();

		for (Integer id : listIdPromo) {
			Promotion p = remotedAnnuaire.findPromoById(id, false);
			StringBuilder newTitle = new StringBuilder();
			newTitle.append(p.getPromoYear());
			newTitle.append(" : ");
			newTitle.append(p.getTitle());
			p.setTitle(newTitle.toString());
			listPromo.add(p);

		}

		map.put("listPromo", listPromo);

		return map;

	}

	/**
	 * @param remotedAnnuaire
	 */
	public void setRemotedAnnuaire(IRemotedAnnuaire remotedAnnuaire) {
		this.remotedAnnuaire = remotedAnnuaire;
	}

	/**
	 * @return
	 */
	public IRemotedAnnuaire getRemotedAnnuaire() {
		return remotedAnnuaire;
	}

}
